<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Api\AngelRoleApi;
use Admin\Api\DatatreeApi;
use Admin\Api\PartnerRoleApi;
use Admin\Api\PostApi;
use Admin\Api\StockTimeApi;
use Admin\Api\VPostInfoApi;
use Admin\Model\DatatreeModel;
use Uclient\Api\UserApi;
use Admin\Api\RoleApi;
class VipGradeController extends  AdminController{

    /*
     * 会员等级参数设置
     * */
    public function parameterset(){
        $this->display();
    }

    /*
     * 增加会员等级
     * */
    public function addvip(){
        if(IS_POST){
            $name = I('name');
            if(empty($name)){
                $this->error('VIP名称缺失');
            }
            $add = array(
                'name' => $name,
                'vip_type' => 1,
                'all_chance' => I('all_chance','0'),
                'chance' => I('all_chance','0'),//0表示你没有次数限制
                'save_money' => I('save_money','0'),
                'consume_money' => I('consume_money','0'),
                'discount_threshold_value' => I('discount_threshold_value','0'),//0表示没有限制
                'discount' => I('discount',1),
                'integral_multiple' => I('integral_multiple',1),
                'become_gold' => I('become_gold',0),
                'become_platina' => I('become_platina',0),
                'vip_reward' => I('vip_reward',0),
                'consume_reward' => I('consume_reward',0),
                'cumulative' => I('cumulative',100000),
                'cumulative_money' => I('cumulative_money',100000),
                'cumulative_number' => I('cumulative_number',0),
                'inv_vip_reward' => I('inv_vip_reward',0),
                'inv_consume_reward' => I('inv_consume_reward',0),
                'reward' => I('reward',0),
                'cumulative_all_number' => I('cumulative_all_number',0),
                'create_time' => time()
            );
            $res = apiCall(RoleApi::ADD,array($add));
            if($res['status']){
                $this->success('添加成功',U('VipGrade/vipgradelist'));
            }else{
                $this->error('添加失败',U('VipGrade/addgrade'));
            }

        }else{
            $this->display();
        }
    }

    /*
     * 会员等级列表
     * */
    public function vipgradelist(){
        $info = apiCall(RoleApi::QUERY_NO_PAGING);
        if($info['status']){
            $this->assign('list',$info['info']);
        }
        $this->display();
    }

    /*
     * 天使VIP等级列表
     * */
    public function angel_viplist(){
        $info = apiCall(AngelRoleApi::QUERY_NO_PAGING,array(array('id'=>'1')));
        if($info['status']){
            $this->assign('list',$info['info']);
        }
        $this->display();
    }

    /*
     * 编辑天使VIP
     * */
    public function edit_angelvip(){
        $map['id'] = I('id');
        if(IS_POST){
            $entity = array(
                'name' => I('name'),
                'lower_limit' => I('lower_limit'),
                'upper_limit' => I('upper_limit'),
                'discount' => I('discount'),
                'become_gold' => I('become_gold'),
                'become_platina' => I('become_platina'),
                'reward' => I('reward'),
                'quota_upper' => I('quota_upper'),
                'quota_overplus' => I('quota_overplus'),
                'vip_reward' => I('vip_reward'),
                'consume_reward' => I('consume_reward'),
                'momth_recommend' => I('year_recommend'),
                'create_time' => time(),
            );
            $res = apiCall(AngelRoleApi::SAVE,array($map,$entity));
            if($res['status']){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $res = apiCall(AngelRoleApi::GET_INFO,array($map));

            if($res['status']){
                $this->assign($res['info']);
            }
            $this->display();
        }
    }

    /*
     * 股东VIP等级列表
     * */
    public function  partner_viplist(){
        $info = apiCall(PartnerRoleApi::QUERY_NO_PAGING,array(array('id'=>'1')));
        if($info['status']){
            $this->assign('list',$info['info']);
        }
        $this->display();
    }

    /*
     * 股东vip
     * */
    public function edit_partnervip(){
        $map['id'] = I('id');
        if(IS_POST){
            $entity = array(
                'name' => I('name',''),
                'lump_sum_payment' => I('lump_sum_payment',''),
                'upper_limit' => I('upper_limit',''),
                'discount' => I('discount',''),
                'become_gold' => I('become_gold',''),
                'become_platina' => I('become_platina',''),
                'reward' => I('reward',''),
                'quota_upper' => I('quota_upper',''),
                'quota_overplus' => I('quota_overplus',''),
                'vip_reward' => I('vip_reward',''),
                'consume_reward' => I('consume_reward',''),
                'momth_recommend' => I('year_recommend','0'),
                'year_recommend' => I('year_recommend','0'),
                'create_time' => time(),
            );

            $res = apiCall(PartnerRoleApi::SAVE,array($map,$entity));
            if($res['status']){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $res = apiCall(PartnerRoleApi::GET_INFO,array($map));
            if($res['status']){
                $this->assign($res['info']);
            }
            $this->display();
        }
    }

    /*
     * 编辑会员等级
     * */
    public function edit(){
        $id = I('id');
        $map['id'] = $id;
        if(IS_POST){
            $name = I('name');
            if(empty($name)){
                $this->error('VIP名称缺失');
            }
            $entity = array(
                'name' => $name,
                'all_chance' => I('all_chance','0'),
                'chance' => I('all_chance','0'),//0表示你没有次数限制
                'save_money' => I('save_money','0'),
                'consume_money' => I('consume_money','0'),
                'discount_threshold_value' => I('discount_threshold_value','0'),//0表示没有限制
                'discount' => I('discount',1),
                'integral_multiple' => I('integral_multiple',1),
                'become_gold' => I('become_gold',0),
                'become_platina' => I('become_platina',0),
                'vip_reward' => I('vip_reward',0),
                'consume_reward' => I('consume_reward',0),
                'obtain_points' => I('obtain_points',0),
                'cumulative' => I('cumulative',100000),
                'cumulative_money' => I('cumulative_money',100000),
                'cumulative_number' => I('cumulative_number',0),
                'reward' => I('reward',0),
                'cumulative_all_number' => I('cumulative_all_number',0),
                'create_time' => time()
            );
            $res = apiCall(RoleApi::SAVE,array($map,$entity));
            if($res['status']){
                if($id == 3){
                    $angle_down_rolemap['id'] = 2;
                    $asave['discount'] = I('discount',1);
                    $ares = apiCall(AngelRoleApi::SAVE,array($angle_down_rolemap,$asave));

                    $parnter_down_rolemap['id'] = 2;
                    $psave['discount'] = I('discount',1);
                    $pres = apiCall(PartnerRoleApi::SAVE,array($parnter_down_rolemap,$psave));
                }

                $this->success('修改成功',U('VipGrade/vipgradelist'));
            }else{
                $this->error('修改失败',U('VipGrade/vipgradelist'));
            }
        }else{
            $info = apiCall(RoleApi::GET_INFO,array($map));
            if($info['status']){
                $this->assign($info['info']);
            }
            $this->display();
        }
    }



    /*
     * 消费时间规定
     * */
    public function consume_stock(){
        $map = array(
            'id'=>'1'
        );
        if(IS_POST){
            $starttime = strtotime(I('start_time'));
            $stoptime = strtotime(I('end_time'));
            $save = array(
                'start_time'=>$starttime,
                'stop_time'=>$stoptime
            );
            $res = apiCall(StockTimeApi::SAVE,array($map,$save));
            if($res['status']){
                $this->success('设置成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $info = apiCall(StockTimeApi::GET_INFO,array($map));
            if($info['status']){
               $this->assign('info',$info['info']);
            }
            $this->display();
        }

    }

}